{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open('dataset.json') as fopen:\n",
    "    dataset = json.load(fopen)\n",
    "    \n",
    "X = dataset['train_X'] + dataset['test_X']\n",
    "Y = dataset['train_Y'] + dataset['test_Y']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import youtokentome as yttm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('text.txt', 'w') as fopen:\n",
    "    fopen.write('\\n'.join(X + Y))\n",
    "    \n",
    "bpe = yttm.BPE.train(data='text.txt', vocab_size=32000, model='bpe.model',\n",
    "               pad_id=0, unk_id=2, bos_id=3, eos_id=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_X = bpe.encode(dataset['train_X'], output_type=yttm.OutputType.ID)\n",
    "train_Y = bpe.encode(dataset['train_Y'], output_type=yttm.OutputType.ID)\n",
    "test_X = bpe.encode(dataset['test_X'], output_type=yttm.OutputType.ID)\n",
    "test_Y = bpe.encode(dataset['test_Y'], output_type=yttm.OutputType.ID)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('dataset-bpe.json', 'w') as fopen:\n",
    "    json.dump({'train_X': train_X, 'train_Y': train_Y, 'test_X': test_X, 'test_Y': test_Y}, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
